package DataStruct;

import android.util.Log;

/**
 * @param <E>
 */
@SuppressWarnings("unchecked")
public class ArrayList<E> extends AbstractList<E> {
    /**
     * 存放元素容器
     */
    private E[] elements;

    /**
     * 数组默认容量
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * 数组动态扩容
     * 目前需要容量.
     * lianglibao
     * 2019/5/22
     * 4:40 PM
     */
    private void ensureCapacity() {
        int oldCapacity = elements.length;
        if (oldCapacity >= (size + 1)) return;

        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        Log.d("ArrayList", oldCapacity + "扩容为" + newCapacity);
    }

    /**
     * 数组动态缩容
     * lianglibao
     * 2019/5/23
     * 3:37 PM
     */
    private void term () {
        int oldCapacity = elements.length;
        int newCapacity = oldCapacity >> 1;
        if (size >= newCapacity || oldCapacity <= DEFAULT_CAPACITY ) return;

        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < newCapacity; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        Log.d("ArrayList", oldCapacity + "缩容为" + newCapacity);
    }


    /**
     * 构造方法
     * 数组容量
     * lianglibao
     * 2019/5/22
     * 3:49 PM
     */
    public ArrayList(int capacity) {
        capacity = (capacity < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capacity;
        elements = (E[]) new Object[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

     /**
       * 获取数组元素个数
       * lianglibao
       * 2019/5/22
       * 3:50 PM
       */
    public int length() {
        return elements.length;
    }

     /**
       * 清空shuzu
       * lianglibao
       * 2019/5/22
       * 4:00 PM
       */
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;

        // 优化部分
        // 如果存放元素的数组很大,此刻清空元素后需要减小数组开辟的内存
        if (elements != null && elements.length > DEFAULT_CAPACITY) {
            elements =  (E[]) new Object[DEFAULT_CAPACITY];
        }
    }

     /**
       * 判断元素是否存在于数组中
       * lianglibao
       * 2019/5/22
       * 4:08 PM
       */
    public int indexOf(E element) {
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (elements[i] == null) return i;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i])) return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 从某个位置添加元素
     * 添加位置index
     * lianglibao
     * 2019/5/22
     * 5:24 PM
     */
    public void add (int index, E element) {
        rangeCheckForAdd(index);

        // 动态扩容
        ensureCapacity();

        // index 位置之后的值向后移!
        for (int i = size; i > index ; i--) {
            elements[i] = elements[i - 1];
        }
        elements[index] = element;
        size ++;
    }

    /**
     * 删除某个位置元素
     * 删除位置
     * lianglibao
     * 2019/5/22
     * 5:24 PM
     */
    public E remove(int index) {
        rangeCheck(index);
        E element = elements[index];
        // index之后的元素向前移动
        for (int i = index + 1; i < size; i++) {
            elements[i - 1] = elements[i];
        }
        // 将size减1,并且将size-1位置的元素置为null;
        elements[--size] = null;

        // 数组缩容
        term();

        return element;
    }

    public E set(int index, E element) {
        rangeCheck(index);

        E oldElement = elements[index];
        elements[index] = element;
        return oldElement;
    }

    public E get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("Size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(elements[i]);
        }
        sb.append("]");
        return sb.toString();
    }
}
